<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/20
 * Time: 14:33
 */
require_once ADMIN_PATH . '../business/createcode.php';
include_once ADMIN_PATH ."../bs/bs.automation.php";
class User_manageController extends BaseController{
    function index(){
        $doUser = new Do用户();
        $userTable = new Do用户推广表();
        $doDoctor = new Do医师详情();
        $doUser->set_公众号ID(G_SERV_ID);
        $userName = $this->request('name');
        $certification = $this->request('certification');
        $follow = $this->request('follow');
        $time = $this->request('date');

        if($userName){
            $where= " name like'%".$userName."%'";
        }
        if($certification)
        {
            $where.=($userName) ? 'and' :'';
            if($certification == 1)
            {
                $where.=' 认证=1';
            }
            else
            {
                $where.=' 认证>1';
            }
        }

        if($follow==='0'){
            $where.=($where) ? ' and' : '';
            $where.=' unsubscribe=0';
        }
        if($follow == '1')
        {
            $where.=($where) ? ' and' : '';
            $where.=' unsubscribe=1';
        }
        if($time){
            $where.=($where) ? ' and' : '';
            $where.= " date_format(regist_time, '%Y-%m-%d')='".$time."'";
        }
        $count = $doUser->where($where)->count();
        $page = new \tools\Page($count,15);
        $followNum = $doUser->where("unsubscribe=0")->count();
        $users = $doUser->fields('*')
            ->where($where)
            ->offset($page->limit)
            ->limit($page->pagesize)
            ->orderby("regist_time desc")
            ->rows();
        foreach($users as $k=>$v){
            $doctor = $doDoctor->fields("id")
                ->where(array('用户id'=>$v['id']))
                ->row()->data;
            $users[$k]['doctor_id'] = $doctor['id'];
            $qrcode = $userTable->fields('二维码编号')->where("用户id = ".$v['id'])->getOneField();

            $users[$k]['二维码编号'] = $qrcode;
            if ($qrcode) {
                $users[$k]['qrcode_url'] = $code = G_二维码查看目录.$qrcode.'.jpg';
            }
        }

        //统计今日新增人数
        $doUser = new Do用户();
        $end = date("Y-m-d H:i:s", time());
        $date = substr($end, 0, 10);
        $start = $date . ' ' . '00:00:00';
        $newAdd = $doUser->where(" unsubscribe=0 and regist_time  BETWEEN '" . $start . "'  AND '" . $end . "'")->count();
        require $this->template;
    }

    function update_user_status(){
        $doUser = new Do用户();
        $doUser->set_id($this->request('userId','',true));
        $status = $this->request('status');
        $userStatus = ($status==1) ? 2 : 1;
        $doUser->set_是否正常($userStatus);
        if ($doUser->save()) {
            $this->jsonOutPut(0, '修改成功');
        } else {
            $this->jsonOutPut(-1, '修改失败');
        }
    }

    function generate_user_code(){
        $userId = $this->request('sid','',true);
        $bm = new C后台管理员();
        $返回数据=$bm->fh生成用户推广二维码($userId);
        echo $返回数据;
    }

    function send_message(){
        $doUser = new Do用户();
        $user = $doUser->fields('telephone')->where('公众号ID='.G_SERV_ID.' and telephone !=""')->rows();
        $content = '626医师节，我们携手数十位儿科大咖诚邀您，一起寻找最美儿科医生，还有万元大奖，http://t.cn/R5WREBy 回T退订';
        $send = new automation();
        foreach($user as $v){
           $send->send_messages($content, $v['telephone']);
        }

    }

    function chart() {
        $doUser = new Do用户();

        #省
        $doDocter = new Do医师详情();
        $list1 = $doDocter->fields("trim(u.province) name, count(*) value")
            ->leftJoin($doUser, 'u', 'a.用户id = u.id')
            ->where("a.`状态` = '通过' and u.province !=''")
            ->groupby('trim(u.province)')
            ->orderby("value desc")
            ->rows();
        $province = json_encode($list1);
        #科室
        $doDocter = new Do医师详情();
        $list2 = $doDocter->fields("count(*) value, trim(专业领域) name")
            ->where("`状态` = '通过' and 专业领域 != '' ")
            ->groupby('trim(专业领域)')
            ->orderby("value desc")
            ->rows();
        $department = json_encode($list2);
        #职称
        $doDocter = new Do医师详情();
        $list3 = $doDocter->fields("count(*) value, trim(职务) name")
            ->where("a.`状态` = '通过' and 职务 != ''")
            ->groupby('trim(职务)')
            ->orderby("value desc")
            ->rows();
        $professor = json_encode($list3);
        require $this->template;
    }
    function province_hospital(){
        $province = $this->request('province', '', true);
        $doUser = new Do用户();
        $doDoctor = new Do医师详情();
        $list = $doUser->fields("count(*) value, trim(d.医院) name")
            ->leftJoin($doDoctor, 'd', 'd.用户id = a.id')
            ->where("d.状态='通过' and a.province='".$province."'")
            ->groupby('trim(d.医院)')
            ->orderby('value desc')
            ->rows();
        $hospital = json_encode($list);
        echo $hospital;
    }

    function expert_manage(){
        $doExpert = new Do专家();
        $doUser = new Do用户();
        $doDoctor = new Do医师详情();
        $doQrcode = new Do用户推广表();
        $count = $doExpert->count();
        $page = new \tools\Page($count, 15);
        $experts = $doExpert->offset($page->limit)
            ->limit($page->pagesize)
            ->rows();
        foreach($experts as $k=>$v){
            $user = $doUser->fields("头像")->row($v['id'])->data;
            $doDoctor->set_用户id($v['id']);
            $doctor = $doDoctor->fields("医院")->where($doDoctor->data)->row()->data;
            $experts[$k]['portrait'] = $user['头像'];
            $experts[$k]['hospital'] = $doctor['医院'];
            $qrcode = $doQrcode->fields('二维码编号')->where("用户id = ".$v['id'])->row()->data;
            $code = G_二维码查看目录.$qrcode['二维码编号'].'.jpg';
            $experts[$k]['code'] = $code;
        }
        require $this->template;
    }

    public function addExpert(){
        require $this->template;
    }
    public function addExpertPost(){
        $doExpert = new Do专家();
        $doUser = new Do用户();
        $expertId = $this->request('专家id','', true);
        $expert = $doExpert->where(array('id'=>$expertId))->count();
        $list_url = $this->url('user_manage', 'expert_manage');
        if($expert){
            echo "<script>alert('专家已存在！');location.href='".$list_url."'</script>";
            return false;
        }
        $user = $doUser->fields("name")->row($expertId)->data;
        $name = $user['name'];
        $doExpert->set_id($expertId);
        $doExpert->set_name($name);
        $res = pdo()->insert('专家', array('id'=>$expertId, 'name'=>$name));
        if($res){
            echo "<script>alert('提交成功！');location.href='".$list_url."'</script>";
        }else{
            echo "<script>alert('提交失败！');</script>";
        }

    }

    public function del(){
        $expertId = $this->request('expert_id', '', true);
        $page = $this->request('page');
        $doExpert = new Do专家();
        $doExpert->set_id($expertId);
        if($doExpert->delete()){
            $this->redirect('user_manage', 'expert_manage', array('page'=>$page));
        }
    }



}